package com.xzh.homeliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xzh.homeliving.commodity.entity.AtEntity;
import com.xzh.homeliving.commodity.entity.AttrAtRelationEntity;
import com.xzh.homeliving.commodity.service.AtService;
import com.xzh.homeliving.commodity.service.CategoryService;
import com.xzh.homeliving.commodity.vo.AttrGroupWithAttrsVo;
import org.springframework.web.bind.annotation.*;

import com.xzh.homeliving.commodity.entity.AttrEntity;
import com.xzh.homeliving.commodity.service.AttrService;
import com.xzh.common.utils.PageUtils;
import com.xzh.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author xzh
 * @email ranpis02@qq.com
 * @date 2024-01-27 23:25:49
 */
@RestController
@RequestMapping("commodity/attr")
public class AttrController {
    @Resource
    private AttrService attrService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AtService atService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息,通过级联选择器获取相应信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("id") Long id){
		AttrEntity attr = attrService.getById(id);
        Long categoryId = attr.getCategoryId();
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);
        attr.setCascadedCategoryId(cascadedCategoryId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] ids){
		attrService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据业务需求，增加根据第三级分类+查询条件来得到对应列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 根据属性组 id, 返回该属性组关联的基本属性
     */
    @RequestMapping("/{attrGroupId}/at/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AtEntity> relationAttr = atService.getRelationAttr(attrGroupId);
        return R.ok().put("data", relationAttr);
    }

    /**
     * 根据属性id和属性组id来删除其在关联表中的关联关系
     */
    @RequestMapping("/at/relation/delete")
    public R deleteRelation(@RequestBody AttrAtRelationEntity[] attrAtRelationEntities) {
        atService.deleteAttrGroupRelation(attrAtRelationEntities);
        return R.ok();
    }

    /**
     * 根据属性组id返回可以关联的基本属性
     */
    @RequestMapping("/{attrGroupId}/at/allowrelation")
    public R getBaseAttrFromAttrGroup(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = atService.getBaseAttrFromAttrGroup(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 根据分类id返回属性和属性组的关联
     */
    @RequestMapping("/{catalogId}/withat")
    public R getAttrGroupWithAttrs(@PathVariable("catalogId") Long catalogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrs = attrService.getAttrGroupWithAttrs(catalogId);
        return R.ok().put("data", attrGroupWithAttrs);
    }
}
